﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using COFE.Core;
using SevenZip;

namespace COFE.IO.SZ
{
    public class SevenZipPropertyProvider : PropertyProviderBase<SZEntryInfo>
    {
        #region Constructor
        public SevenZipPropertyProvider(SevenZipDirectoryLister volume, SZEntryInfo entry)
            : base(entry)
        {
            _volumeKey = volume.ListerKey;
            registerProperties(entry);            
            
        }
        #endregion

        #region Methods

        private void registerProperties(SZEntryInfo entry)
        {
            //base.RegisterMethod(FileSystemInfoProperties.FullName, (e,p) => { return e.FullName; });
            base.RegisterMethod(FileSystemInfoProperties.FullParseName, (e, p) => { return e.ParseName; });

            base.RegisterMethod(FileSystemInfoProperties.ParentParseName, (e, p) =>
            {
                string parentPath = COFEPath.GetDirectoryName(this.GetProperty(FileSystemInfoProperties.FullParseName).ValueAsString);
                return (string.IsNullOrEmpty(parentPath)) ? null : parentPath;
            });

            //{ return _rootPath == null ? e.FullName : IOTools.GetRelativePath(e, rootDir); }); 
            //IOTools.GetRelativePath do not support Guid path.
            base.RegisterMethod(FileSystemInfoProperties.Name, (e, p) => { return e.Name; });            
            base.RegisterMethod(FileSystemInfoProperties.Label, (e, p) => { return e.Name; });
            base.RegisterMethod(FileSystemInfoProperties.SecondaryVolumeKey, (e, p) => { return _volumeKey; });

            base.RegisterMethod(FileSystemInfoProperties.IsExists, (e, p) => { return entry.RootWrapper.exists(e.RelativePath, e.IsFolder); });
            base.RegisterMethod(FileSystemInfoProperties.IsFileSystem, (e, p) => { return false; });
            base.RegisterMethod(FileSystemInfoProperties.FileAttributes, (e, p) => {
                return e.IsFolder ? e.RootWrapper.ArchiveFile.Attributes | FileAttributes.Directory : e.RootWrapper.ArchiveFile.Attributes;
                 });            
            base.RegisterMethod(FileSystemInfoProperties.COFEAttributes, (e, p) =>
            {
                return e.IsFolder ? COFEAttributes.FileSystem | COFEAttributes.SubEntriesCanChange | COFEAttributes.Browsable :
                  COFEAttributes.FileSystem;
            });

            if (!entry.IsFolder)
            {
                base.RegisterMethod(FileInfoProperties.Length, (e, p) => { return (long)e.CurrentItem.Size; });
                base.RegisterMethod(FileInfoProperties.CRC, (e, p) => { return String.Format("{0:X}", e.CurrentItem.Crc); });
            }
            else
            {
                RegisterMethod(DirectoryInfoProperties.HasSubFolders, (e, p) => { return true; });
                RegisterMethod(DirectoryInfoProperties.HasFiles, (e, p) => { return true; });
                RegisterMethod(DirectoryInfoProperties.DirectoryType, (e, p) => { return DirectoryTypeEnum.dtFolder; });
            }

            base.RegisterMethod(FileSystemInfoProperties.CreationTimeUtc,
                (e, p) => { return e.CurrentItem.CreationTime; });
            base.RegisterMethod(FileSystemInfoProperties.LastAccessTimeUtc,
                (e, p) => { return e.CurrentItem.LastAccessTime; });
            base.RegisterMethod(FileSystemInfoProperties.LastWriteTimeUtc,
                (e, p) => { return e.CurrentItem.LastWriteTime; });

            //if (!entries.IsFolder)
            //{
            //    base.RegisterMethod(IconProperties.Icon,
            //        (e,p) =>
            //        {
            //            return Icon.ExtractAssociatedIcon(e.Name).ToBitmap().EnsureBitmapSize(IconProperties.Icon);
            //        });
            //}


        }

        #endregion

        #region Data

        private string _volumeKey;

        #endregion

        #region Public Properties

        #endregion

    }
}
